package com.py.credit.controller;

import com.py.framework.core.controller.BaseController;
import com.py.framework.core.helper.HttpServletHelper;
import com.py.framework.core.helper.JWTHelper;
import com.py.framework.core.log.ExceptionLogger;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统基础控制器
 * @author Leegern
 * @date   2018年4月20日
 */
public class SystemController extends BaseController {
	
	
	/**
	 * 字符串转成Long
	 * @param str
	 * @return
	 */
	protected List<Long> convert2Long(String str) {
		List<Long> result = null;
		if (StringUtils.isNotBlank(str)) {
			String[] array = str.split(",");
			if (null != array && array.length > 0) {
				result = new ArrayList<>(array.length);
				for (int i = 0; i < array.length; i++) {
					result.add(Long.parseLong(array[i]));
				}
			}
		}
		return result;
	}
	
	/**
	 * 解密用户Id
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	protected long pickUserId(HttpServletRequest request) {
		long userId = 0L;
		String jdToken = HttpServletHelper.getUserToken(request);
		if (StringUtils.isNotBlank(jdToken)) {
			try {
				Claims claims = JWTHelper.parseJWT(jdToken);
				if (null != claims && StringUtils.isNotBlank(claims.getSubject())) {
					userId = Long.parseLong(claims.getId());
				}
			} catch (Exception e) {
				ExceptionLogger.error(e);
			}
		}
		return userId;
	}
}